﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.IO;


public partial class CMS_Control_HtmlEditor : System.Web.UI.UserControl
{
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();
    Usas.Common.Cms.FileControl fc = new Usas.Common.Cms.FileControl();
    private int _FckHeight = 500;
    public int FckHeight
    {
        get
        {
            return _FckHeight;
        }
        set
        {
            _FckHeight = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (_FckHeight == 500)
            {
                FckDiv.Style.Add("height", FckHeight.ToString() + "px");
            }
        }
    }

    public void setFck(int Height)
    {
        Fck.Height = Unit.Pixel(Height);
    }

    public FileUpload GetFileUpload
    {
        get
        {
            return FileLoad;
        }
    }

    public string GetValue()
    {
        return Fck.Value;
    }

    /// <summary>
    /// 内容区赋值
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <param name="dir">目录</param>
    /// <param name="name">标题名</param>
    public void SetValue(string filename, string dir, string name)
    {
        try
        {
            if (_FckHeight == 500)
            {
                FckDiv.Style.Add("height", FckHeight.ToString() + "px");
            }


            string temppath = ConfigurationSettings.AppSettings["UsasInformationPath"] + "CMSUpLoadFiles\\";
            if (temppath.IndexOf(":") == -1)
            {
                temppath = MapPath(temppath + dir + "\\" + filename);
            }
            else
            {
                temppath += dir + "\\" + filename;
            }

            if (!fc.IsData(filename))
            {
                plusfile.Visible = true;
                lbtn.CommandArgument = temppath;
                lbtn.Text = name;
            }
            else
            {
                string msg = File.ReadAllText(temppath, System.Text.Encoding.GetEncoding("UTF-8")).ToString();
                Fck.Value = msg;

                plusfile.Style.Add("visibility", "hidden");
            }

        }
        catch
        {
            Fck.Value = "";
            plusfile.Style.Add("visibility", "hidden");
        }
    }

    /// <summary>
    /// FCK初始化（ONMOUSE增加事件） 
    /// </summary>
    public void FckInit()
    {
        _FckHeight = 500;
    }


    protected void lbtn_Click(object sender, EventArgs e)
    {
        if (File.Exists(lbtn.CommandArgument))
        {
            FileInfo fn = new System.IO.FileInfo(lbtn.CommandArgument);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('文件不存在')", true);
        }
    }

    public void ShowFck(bool IsHtml)
    {
        if (IsHtml)
        {
            FckDiv.Visible = true;
            PlusDiv.Visible = false;
        }
        else
        {
            PlusDiv.Visible = true;
            FckDiv.Visible = false;
        }
    }
}
